package com.baiyinet.di.module;

import android.app.Application;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.baiyinet.BuildConfig;
import com.baiyinet.MyApp;
import com.baiyinet.biz.CurrentUser;
import com.baiyinet.rest.Api;
import com.baiyinet.rest.ApiHelper;
import com.baiyinet.rest.CustomTrust;
import com.baiyinet.rest.GetCookiesInterceptor;
import com.baiyinet.rest.UnSafeHostnameVerifier;
import com.baiyinet.util.DoubleClickExitDetector;
import com.baiyinet.util.Utils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader;
import com.bumptech.glide.load.model.GlideUrl;

import java.io.InputStream;
import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import autodagger.AutoExpose;
import cn.jpush.android.api.JPushInterface;
import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author J.Huang
 * @version 1.0
 * @since 16/11/23
 */
@Module
public class AppModule {
    private Application mApp;

    public AppModule(Application app) {
        mApp = app;
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    SharedPreferences provideSharedPreferences() {
        return PreferenceManager.getDefaultSharedPreferences(mApp);
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    Api provideApi(SharedPreferences sharedPreferences, CustomTrust customTrust, UnSafeHostnameVerifier hostnameVerifier) {
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        httpClientBuilder
                .connectTimeout(100, TimeUnit.SECONDS)
                .writeTimeout(100, TimeUnit.SECONDS)
                .readTimeout(300, TimeUnit.SECONDS)
                .addInterceptor(httpLoggingInterceptor)
                .addInterceptor(new GetCookiesInterceptor(sharedPreferences));
//                .addInterceptor(httpInterceptor)

        // 添加https
        httpClientBuilder
                .sslSocketFactory(customTrust.sslSocketFactory, customTrust.trustManager)
                .hostnameVerifier(hostnameVerifier);

        OkHttpClient httpClient = httpClientBuilder.build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BuildConfig.ENDPOINT)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(httpClient)
                .build();

        Glide.get(mApp).register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(httpClient));

        return retrofit.create(Api.class);
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    ApiHelper provideApiHelper(Api api, SharedPreferences sharedPref) {
        String regId = JPushInterface.getRegistrationID(mApp);
        return new ApiHelper(api, sharedPref, regId);
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    CurrentUser provideCurrentUser(SharedPreferences sharedPref) {
        return new CurrentUser(sharedPref);
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    DoubleClickExitDetector provideDoubleClickExitDetector() {
        return new DoubleClickExitDetector(mApp);
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    UnSafeHostnameVerifier provideUnSafeHostnameVerifier() {
        return new UnSafeHostnameVerifier();
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    CustomTrust provideCustomTrust() {
        return new CustomTrust(mApp);
    }
}
